# coding: utf-8
from markdown import markdown
import bleach
from exts import db
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from app import app



class Groupq(db.Model):
    __tablename__ = 'groupq'

    g_id = db.Column(db.Integer, primary_key=True)
    g_name = db.Column(db.String(20), nullable=False, unique=True)
    g_rid = db.Column(db.Numeric(1, 0))



class Userinfo(db.Model):
    __tablename__ = 'userinfo'

    u_id = db.Column(db.Integer, primary_key=True)
    g_id = db.Column(db.ForeignKey('groupq.g_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    u_name = db.Column(db.String(20), nullable=False, unique=True)
    u_pwd = db.Column(db.String(128), nullable=False)
    u_mail = db.Column(db.String(64), nullable=False, unique=True)
    u_img = db.Column(db.String(128))
    u_self=db.Column(db.String(128))
    u_exp=db.Column(db.Integer)

    g = db.relationship('Groupq', primaryjoin='Userinfo.g_id == Groupq.g_id', backref='userinfos')

    def set_pwd(self,pwd):
        self.u_pwd=generate_password_hash(pwd)

    def check_pwd(self,pwd):
        return check_password_hash(self.u_pwd,pwd)

    def exp_up(self,num):
        self.u_exp+=num
        if self.g_id<13:
            while self.u_exp>self.exp_need[self.g_id]:
                self.u_exp-=self.exp_need[self.g_id]
                self.g_id+=1
        

    exp_need=(1000,1,5,15,30,50,100,200,500,1000,2000,3000,5000)

    '''
    @staticmethod
    def levelup(mapper, connection, target):
        exp_need=(1000,1,5,15,30,50,100,200,500,1000,2000,3000,5000)
        if target.g_id<13:
            if target.u_exp>exp_need[target.g_id]:
                target.u_exp-=exp_need[target.g_id]
                target.g_id+=1

    @staticmethod
    def on_insert(mapper, connection, target):
        target.g_id=1
    '''

class Fileq(db.Model):
    __tablename__ = 'fileq'

    f_id = db.Column(db.Integer, primary_key=True)
    u_id = db.Column(db.ForeignKey('userinfo.u_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    f_root = db.Column(db.Text, nullable=False)
    f_name = db.Column(db.String(255), nullable=False, index=True)
    f_size = db.Column(db.Integer, index=True)
    f_dnum = db.Column(db.Integer, index=True)
    uptime = db.Column(db.DateTime,index=True,default=datetime.now)

    u = db.relationship('Userinfo', primaryjoin='Fileq.u_id == Userinfo.u_id', backref='fileqs')



class Downloadq(db.Model):
    __tablename__ = 'downloadq'

    dl_id = db.Column(db.Integer, primary_key=True)
    u_id = db.Column(db.ForeignKey('userinfo.u_id', ondelete='RESTRICT', onupdate='RESTRICT'), index=True)
    f_id = db.Column(db.ForeignKey('fileq.f_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    dl_time = db.Column(db.DateTime,default=datetime.now)

    f = db.relationship('Fileq', primaryjoin='Downloadq.f_id == Fileq.f_id', backref='downloadqs')
    u = db.relationship('Userinfo', primaryjoin='Downloadq.u_id == Userinfo.u_id', backref='downloadqs')

    @staticmethod
    def adddqc(mapper, connection, target):
        sql="update fileq set f_dnum=f_dnum+1"


class Forumq(db.Model):
    __tablename__="forumq"

    fo_id=db.Column(db.Integer,primary_key=True)
    u_id=db.Column(db.ForeignKey('userinfo.u_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    fo_name=db.Column(db.String(32),nullable=False,unique=True)
    fo_parent_id=db.Column(db.ForeignKey('forumq.fo_id', ondelete='RESTRICT', onupdate='RESTRICT'))
    fo_descript=db.Column(db.Text)

    u= db.relationship('Userinfo', primaryjoin='Forumq.u_id == Userinfo.u_id', backref='forumqs')
    fo_parent= db.relationship('Forumq',remote_side=[fo_id], primaryjoin='Forumq.fo_id == Forumq.fo_parent_id', backref='forumqs')


class Articleq(db.Model):
    __tablename__="articleq"

    a_id=db.Column(db.Integer,primary_key=True)
    u_id=db.Column(db.ForeignKey('userinfo.u_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    a_title=db.Column(db.String(128),nullable=False)
    a_context=db.Column(db.Text,nullable=False)
    a_vnum=db.Column(db.Integer,index=True)
    a_rnum=db.Column(db.Integer,index=True)
    a_uptime = db.Column(db.DateTime,index=True,default=datetime.now)
    a_html=db.Column(db.Text)
    fo_id=db.Column(db.ForeignKey('forumq.fo_id', ondelete='RESTRICT', onupdate='RESTRICT'),index=True)
    a_img=db.Column(db.String(2048))

    u= db.relationship('Userinfo', primaryjoin='Articleq.u_id == Userinfo.u_id', backref='articleqs')
    fo=db.relationship('Forumq', primaryjoin='Forumq.fo_id == Articleq.fo_id', backref='articleqs')

    @staticmethod
    def on_body_change2(target,value,oldvalue,initiator):
        allowed_tags=['a','ul','strong','p','h1','h2','h3','img','code']
        html_content=markdown(value,output_format='html')
        html_content=bleach.clean(html_content,tags=allowed_tags,strip=True)
        html_content=bleach.linkify(html_content)
        target.a_html=html_content




class Reviewq(db.Model):
    __tablename__ = 'reviewq'

    r_id = db.Column(db.Integer, primary_key=True)
    f_id = db.Column(db.ForeignKey('fileq.f_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    u_id = db.Column(db.ForeignKey('userinfo.u_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    r_content = db.Column(db.Text, nullable=False)
    r_time = db.Column(db.DateTime,index=True,default=datetime.now)

    f = db.relationship('Fileq', primaryjoin='Reviewq.f_id == Fileq.f_id', backref='reviewqs')
    u = db.relationship('Userinfo', primaryjoin='Reviewq.u_id == Userinfo.u_id', backref='reviewqs')

class Commentsq(db.Model):
    __tablename__ = 'commentsq'

    c_id = db.Column(db.Integer, primary_key=True)
    a_id = db.Column(db.ForeignKey('articleq.a_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    u_id = db.Column(db.ForeignKey('userinfo.u_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False, index=True)
    c_content = db.Column(db.Text, nullable=False)
    html_content=db.Column(db.Text)
    c_time = db.Column(db.DateTime,index=True,default=datetime.now)

    f = db.relationship('Articleq', primaryjoin='Commentsq.a_id == Articleq.a_id', backref='commentsqs')
    u = db.relationship('Userinfo', primaryjoin='Commentsq.u_id == Userinfo.u_id', backref='commentsqs')

    @staticmethod
    def on_body_change(target,value,oldvalue,initiator):
        allowed_tags=['a','ul','strong','p','h1','h2','h3','img']
        html_content=markdown(value,output_format='html')
        html_content=bleach.clean(html_content,tags=allowed_tags,strip=True)
        html_content=bleach.linkify(html_content)
        target.html_content=html_content
 
db.event.listen(Commentsq.c_content,'set',Commentsq.on_body_change)



 
db.event.listen(Articleq.a_context,'set',Articleq.on_body_change2)


        


#db.event.listen(Userinfo,'before_insert',Userinfo.on_insert)



db.event.listen(Downloadq,'before_insert',Downloadq.adddqc)


db.create_all(app=app) 